package com.suredy.app.notice.ctrl;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.suredy.app.notice.model.Notice;
import com.suredy.app.notice.service.NoticeSrv;
import com.suredy.config.model.DocTemplate;
import com.suredy.config.service.DocTemplateSrv;
import com.suredy.core.ctrl.BaseCtrl;
import com.suredy.tools.PageUtils;

/**
 * 公告页面类
 * 
 * @author VIVID.G
 * @since 2015-4-13
 * @version v0.1
 */
@Controller
@RequestMapping("notice")
public class NoticeCtrl extends BaseCtrl {

	private final static Logger log = LoggerFactory.getLogger(NoticeCtrl.class);

	private final int CREATOR_NOTICE = 1;
	private final int VIEW_NOTICE = 2;
	private final String MSG_NOTICE_NOT_FOUND = "未能获取公告数据，请联系管理员协助处理！";

	@Autowired
	private NoticeSrv noticeSrv;
	// TODO: when completed @Autowired
	private DocTemplateSrv docTemplateSrv;

	/**
	 * create a new Notice and start a flow
	 * 
	 * @param fileTypeCode
	 * @return return the new Notice entity's id if created successful
	 */
	@RequestMapping("new")
	@ResponseBody
	public Object newNotice(String fileTypeCode) {
		if (StringUtils.isBlank(fileTypeCode)) {
			log.error("Parameter String[flowCode] is blank.");
			return false;
		}

		// TODO: get file type by fileTypeId and flowCode
		DocTemplate fileType = null;

		if (fileType == null) {
			log.error("Can not find file type by code[" + fileTypeCode + "].");
			return false;
		}

		Notice n = new Notice();

		// TODO: set some init data
		n.setCreateTime(new Date());
		n.setCreatorCode(this.getUser().getUserCode());
		n.setCreatorFullName(this.getUser().getFullName());
		n.setCreatorUnitCode(this.getUser().getUnitCode());
		n.setPublicityPeriod(3); // 默认3天
		n.setTitle("新建的[// TODO: set the file type name]");

		// TODO: replace null to flow code.
		n = this.createFlow(n, fileType.getId(), null, "form/0/{id}.do");

		return this.createTodoUri("form/0/{id}.do", n.getId());
	}

	/**
	 * get notice form
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("form/{readonly}/{id}")
	public ModelAndView getForm(@PathVariable String id, @PathVariable Boolean readonly) {
		ModelAndView view = null;
		if (Boolean.TRUE.equals(readonly))
			view = new ModelAndView("appnotice/view_form");
		else
			view = new ModelAndView("appnotice/form");

		if (StringUtils.isBlank(id)) {
			log.error("Parameter String[id] is blank.");
			return this.gotoErrorPage(this.MSG_NOTICE_NOT_FOUND);
		}

		Notice data = this.noticeSrv.get(id);

		if (data == null) {
			log.error("Can not get Notice data by id[" + id + "].");
			return this.gotoErrorPage(this.MSG_NOTICE_NOT_FOUND);
		}

		view.addObject("data", data);

		return view;
	}

	/**
	 * get notice list
	 * 
	 * @param type
	 * @param page
	 * @return
	 */
	@RequestMapping("list")
	public ModelAndView getNoticeList(Integer type, Integer page) {
		if (type == null)
			type = -1;

		if (page == null || page < 1)
			page = 1;

		ModelAndView view = null;
		Notice search = new Notice();
		LinkedHashMap<String, Boolean> order = new LinkedHashMap<String, Boolean>();

		switch (type) {
			case CREATOR_NOTICE:
				view = new ModelAndView("appnotice/creator_list");
				search.setCreatorCode(this.getUser().getUserCode());
				order.put("createTime", false);
				break;
			case VIEW_NOTICE:
				view = new ModelAndView("appnotice/view_list");
				search.setPublishTime(new Date());
				search.setInPeriod(true);
				break;
			default:
		}

		List<Notice> data = this.noticeSrv.readByEntity(search, page, PageUtils.DEF_PAGE_SIZE, order.isEmpty() ? null : order);
		int count = 0;

		if (data != null && !data.isEmpty()) {
			count = this.noticeSrv.getCountByEntity(search);
			count = count < 0 ? 0 : count;
		}

		view.addObject("pageInfo", PageUtils.newPageInfo(page, count, data));

		// TODO: 需要重新设计查询条件，例如部门公告、公司公告、其他公告之类的，按照用户权限来进行查询
		return view;
	}

	/**
	 * notice list for index page
	 * 
	 * @return
	 */
	@RequestMapping("index_list")
	public ModelAndView getIndexNoticeList() {
		ModelAndView view = new ModelAndView("appindexcontent/notice_list");

		Notice search = new Notice();
		search.setPublishTime(new Date());
		search.setInPeriod(true);

		List<Notice> data = this.noticeSrv.readByEntity(search);

		view.addObject("data", data == null || data.isEmpty() ? null : data);

		return view;
	}

}
